package org.robert.export_client.demo;

import com.robert.common.commonutil.exception.AppException;
import org.apache.commons.collections4.CollectionUtils;
import org.robert.export_client.easyexcel.write.impl.ExcelExportDataService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author robert
 * @Date 2024/1/13 20:03
 **/
@Controller
public class ExcelController {

    /**
     * 导出到浏览器 提供数据
     * @param response
     * @throws IOException
     */
    @GetMapping("/exportDataWeb")
    public void exportDataWeb(HttpServletResponse response) throws Exception {
        List<ExcelTempDemo> list = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)) {
            throw new AppException("500", "暂无数据");
        }
        Map<String, Map<String, String>> dictMap = new HashMap<String, Map<String, String>>();
        HashMap<String, String> map = new HashMap<>();
        map.put("0", "使用");
        map.put("1", "禁用");
        dictMap.put("STATE", map);
        ExcelExportDataService<ExcelTempDemo> excelDownLoad = new ExcelExportDataService<>(response, list, ExcelTempDemo.class, dictMap);
        excelDownLoad.setFileName("测试信息-" + System.currentTimeMillis());
        excelDownLoad.setSheetName("Sheet1");
        excelDownLoad.exportToExcel();
    }

}
